<?php

namespace app\uniapp\controller;


use app\common\exception\MiException;
use app\model\site\User;
use app\model\store\Order;
use app\uniapp\middleware\CheckLogin;
use app\uniapp\service\UniAuthService;
use app\uniapp\service\UserCouponService;
use app\uniapp\service\UserService;
use thans\jwt\facade\JWTAuth;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use think\response\Json;

class UserController extends UniController
{

    protected array $middleware = [
        CheckLogin::class,
    ];

    /**
     * 查询用户信息
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function info(): \think\response\Json
    {
        // 代码执行流程是先走【构造函数】，再执行【中间件】。
        // 刷新登录token是在中间件中进行的，而获取用户信息时在构造函数中，所以【$this->user】的数据先于token获得，有延时
        // 所以这里要重新获取用户信息

        $user = UniAuthService::instance()->getLoginInfo();

        return jsonSuccess([
            'info' => $user,
        ]);
    }

    /**
     * 个人中心首页
     * @return Json
     * @throws DbException
     */
    public function center(): Json
    {

        $uid = UniAuthService::instance()->getLoginId();
        // 待付款订单数量
        $unpaid = Order::where('user_id', $uid)->where('order_status', 'unpaid')->count();
        // 待发货订单数量
        $wait = Order::where('user_id', $uid)->where('order_status', 'wait')->count();
        // 待收货订单数量
        $receive = Order::where('user_id', $uid)->where('order_status', 'receive')->count();
        // 待评价订单数量
        $reply = Order::where('user_id', $uid)->where('order_status', 'reply')->count();

        return jsonSuccess([
            'center' => [
                'unpaid' => $unpaid,
                'wait' => $wait,
                'receive' => $receive,
                'reply' => $reply
            ]
        ]);
    }


    /**
     * 我的优惠券
     * @throws DbException
     */
    public function coupons(): \think\response\Json
    {

        $where = [
            ['user_id', '=', UniAuthService::instance()->getLoginId()],
        ];
        $couponStatus = $this->param('coupon_status');
        $list = UserCouponService::instance()->setWhere($where)->setCouponStatusWhere($couponStatus)->paginate(20);
        return jsonSuccess([
            'items' => $list->items(),
            'total' => $list->total(),
        ]);

    }

    /**
     * 个人专属二维码
     * @return Json
     * @throws MiException
     */
    public  function getWxmpQrcode(): Json
    {
        $qrcode = UserService::instance()->wxMpQrcode(UniAuthService::instance()->getLoginId());

        return jsonSuccess([
            'qrcode' => $qrcode
        ]);
    }


    /**
     * 用户编辑信息
     * @return Json
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function update(): Json
    {
        $post = $this->post();
        $user = UniAuthService::instance()->getLoginInfo();

        if ($post['password']??''){ // 修改密码
            $user->password = md5($post['password']);
        }else{  // 编辑信息
            $user->nickname = $post['nickname'];
            $user->avatar = $post['avatar'];
        }
        $user->save();
        return jsonSuccess([
            'info' => $user
        ]);
    }

}
